/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.view.component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.StringTokenizer;
import org.opu.vcforms.actions.DbConnect;
import org.opu.vcforms.actions.Exit;
import org.opu.vcforms.actions.Statement;
import org.opu.vcforms.actions.Tools;

/**
 *
 * @author yura
 */
public class ToolbarProperties extends ButtonView implements Iterable<String>{

    public static final String ACTION_LABEL = "actions";
    private final Set<String> actions = new LinkedHashSet<String>();

    public ToolbarProperties(GUIProperties prop) {
        super(prop);
        actions.addAll(Arrays.asList(Statement.MNAME, DbConnect.MNAME, Tools.MNAME,
                /*Help.MNAME,*/ Exit.MNAME));
    }

    public Collection<String> parseProperties(String propVal) {
        Collection<String> data = new ArrayList<String>();
        StringTokenizer st = new StringTokenizer(propVal, ";");
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            if (token != null) {
                token = token.trim();
                if (!token.isEmpty()) {
                    data.add(token);
                }
            }
        }
        return data;
    }

    public String reversePropertiesParse(Collection<String> data) {
        StringBuilder sb = new StringBuilder("");
        Iterator<String> iterator = data.iterator();
        while (iterator.hasNext()) {
            sb.append(iterator.next());
            if (iterator.hasNext()) {
                sb.append(';');
            }
        }
        return sb.toString();
    }

    public void setAll(Collection<String> actionNames) {
        String oldVal = reversePropertiesParse(actions);
        removeAll();
        if (actionNames != null && !actionNames.isEmpty()) {
            actions.addAll(actionNames);
        }
        propertyChange(ACTION_LABEL, oldVal, reversePropertiesParse(actions));
//            System.out.println("from [" + oldVal + "] to [" + reversePropertiesParse(actions));
    }

    public void add(String actionName) {
        String oldVal = reversePropertiesParse(actions);
        actions.add(actionName);
        propertyChange(ACTION_LABEL, oldVal, reversePropertiesParse(actions));
    }

    public void removeAll() {
        String oldVal = reversePropertiesParse(actions);
        actions.clear();
        propertyChange(ACTION_LABEL, oldVal, reversePropertiesParse(actions));
    }

    public boolean remove(String o) {
        String oldVal = reversePropertiesParse(actions);
        boolean remove = actions.remove(o);
        propertyChange(ACTION_LABEL, oldVal, reversePropertiesParse(actions));
        return remove;
    }

    @Override
    public Iterator<String> iterator() {
        return actions.iterator();
    }

    @Override
    protected ButtonProperties getProp() {
        return ButtonProperties.getDefaultToolbarButtonProperties();
    }

    @Override
    public String getClassName() {
        return "Toolbar";
    }
}
